package com.cangsan.erhai.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cangsan.erhai.common.PageBean;
import com.cangsan.erhai.common.R;

import com.cangsan.erhai.entity.TabVideo;
import com.cangsan.erhai.service.TabVideoService;
import com.cangsan.erhai.util.StringUtil;
import com.cangsan.erhai.vo.VideoVo;
import com.cangsan.erhai.vo.TabCourseVo;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("/video")
public class TabVideoController {
    @Value("${upload.baseDir}")
    private  String uploadBasedir;


    @Autowired
    private TabVideoService tabVideoService;

    @PostMapping("/list")
    public R list(@RequestBody PageBean pageBean){
        QueryWrapper<VideoVo> queryWrapper = new QueryWrapper<>();
        JSONObject param = pageBean.getParam();


       if(param.containsKey("videoType")){
           String videoType = param.getString("videoType");
           if(StringUtil.isNotEmpty(videoType)){
               queryWrapper.eq("video_type",videoType);
           }

       }
       if(param.containsKey("contestId") && param.containsKey("courseId")){
           Long contestId = param.getLong("contestId");
           Long courseId = param.getLong("courseId");
           if(Objects.nonNull(contestId)){
               queryWrapper.eq("contest_id",contestId);
           }
           if(Objects.nonNull(courseId)){
               queryWrapper.eq("course_id",courseId);
           }

       }
       if(param.containsKey("query")){
           String query = param.getString("query");
           if(StringUtil.isNotEmpty(query)){
               queryWrapper.like("videoName",query);
           }

       }
        Page<VideoVo> page = new Page<>(pageBean.getPageNum(), pageBean.getPageSize());
        IPage<VideoVo> contestVoIPage = tabVideoService.selectTabContestVoPage(page, queryWrapper);

        Map<String,Object> result = new HashMap<>();
        List<VideoVo> records = contestVoIPage.getRecords();
        result.put("dataList",records);
        result.put("total",contestVoIPage.getTotal());
        return R.ok(result);
    }

    @PostMapping("/saveOrUpdate")
    public R saveOrUpdate(@RequestBody TabVideo tabVideo){
        tabVideoService.saveOrUpdate(tabVideo);
        return R.ok();
    }

    @RequestMapping("/uploadVideo")
    public Map<String,Object> uploadVideo(MultipartFile file) throws IOException {
        Map<String,Object> resultMap = new HashMap<>();
        if(!file.isEmpty()){
            String originalFilename = file.getOriginalFilename();
            String suffinxName = originalFilename.substring(originalFilename.lastIndexOf("."));
            String uid = UUID.randomUUID().toString().replaceAll("-", "");
            String newFileName = uid+ DateUtil.format(new Date(),"yyyyMMdd")+suffinxName;
            FileUtils.copyInputStreamToFile(file.getInputStream(),new File(uploadBasedir+"/videoConver",newFileName));
            resultMap.put("code",0);
            resultMap.put("msg","上传成");
            Map<String,Object> dataMap = new HashMap<>();
            dataMap.put("title",newFileName);
            dataMap.put("src","files/videoConver/"+newFileName);
            resultMap.put("data",dataMap);
        }
        return resultMap;
    }

    @RequestMapping("/uploadInco")
    public Map<String,Object> uploadImage(MultipartFile file) throws IOException {
        Map<String,Object> resultMap = new HashMap<>();
        if(!file.isEmpty()){
            String originalFilename = file.getOriginalFilename();
            String suffinxName = originalFilename.substring(originalFilename.lastIndexOf("."));
            String uid = UUID.randomUUID().toString().replaceAll("-", "");
            String newFileName = uid+ DateUtil.format(new Date(),"yyyyMMdd")+suffinxName;
            FileUtils.copyInputStreamToFile(file.getInputStream(),new File(uploadBasedir+"/videoConver",newFileName));
            resultMap.put("code",0);
            resultMap.put("msg","上传成");
            Map<String,Object> dataMap = new HashMap<>();
            dataMap.put("title",newFileName);
            dataMap.put("src","files/videoConver/"+newFileName);
            resultMap.put("data",dataMap);
        }
        return resultMap;
    }

    @GetMapping("/{id}")
    public R delete(@PathVariable Long id) {
        tabVideoService.removeById(id);
        return R.ok("删除成功");
    }

    @GetMapping("getVideoById/{id}")
    public R getVideoById(@PathVariable Long id) {
        TabVideo byId = tabVideoService.getById(id);
        return R.ok(byId);
    }


}
